import 'package:chat_shop/common/const/cosnt.dart';
import 'package:chat_shop/common/widget/bottom_btn.dart';
import 'package:chat_shop/login&regist/view/sms_counter_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_text_view.dart';

class RegistEmailView extends StatefulWidget {
  @override
  _RegistEmailViewState createState() => _RegistEmailViewState();
}

class _RegistEmailViewState extends State<RegistEmailView> {
  String phone = "";
  TextEditingController _controller = TextEditingController();
  TextEditingController _controller1 = TextEditingController();
  TextEditingController _controller2 = TextEditingController();
  TextEditingController _controller3 = TextEditingController();
  TextEditingController _controller4 = TextEditingController();
  FocusNode _focusNode = FocusNode();
  FocusNode _focusNode1 = FocusNode();
  FocusNode _focusNode2 = FocusNode();
  FocusNode _focusNode3 = FocusNode();
  FocusNode _focusNode4 = FocusNode();
  bool isobscue = true;
  void _submit() {
    if (_controller.text.isEmpty) {
      EasyLoading.showError("请输入邮箱号码");
      return;
    }
    if (_controller1.text.isEmpty) {
      EasyLoading.showError("请输入验证码");
      return;
    }
    if (_controller3.text != _controller4.text) {
      EasyLoading.showError("两次密码不一致");
      return;
    }
    EasyLoading.show(status: "Loading...");
    EasyLoading.showSuccess("注册成功");
    SharedPreferences.getInstance().then((value) => value.setString(AppConst.KEY_user_token, "token"));
    Navigator.of(context).pushReplacementNamed('/root');
  }

  @override
  void dispose() {
    _controller.dispose();
    _controller1.dispose();
    _controller2.dispose();
    _controller3.dispose();
    _controller4.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        _focusNode.unfocus();
        _focusNode1.unfocus();
        _focusNode2.unfocus();
        _focusNode3.unfocus();
      },
      child: Column(
        children: [
          LoginTextFieldView(
            hitText: "请输入您的邮箱号",
            controller: _controller,
            focusNode: _focusNode,
            onChanged: (sender) {
              setState(() {
                phone = sender;
              });
            },
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10),
            child: Stack(
              children: [
                LoginTextFieldView(
                  hitText: "请输入验证码",
                  maxlength: 6,
                  controller: _controller1,
                  focusNode: _focusNode1,
                  inputType: TextInputType.number,
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                ),
                Positioned(
                  right: 10,
                  top: 12,
                  child: SmsCounterView(phone: phone),
                )
              ],
            ),
          ),
          Stack(
            children: [
              LoginTextFieldView(
                hitText: "输入登录密码",
                isobser: isobscue,
                controller: _controller3,
                focusNode: _focusNode3,
              ),
              Positioned(
                  right: 10,
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        isobscue = !isobscue;
                      });
                    },
                    child: Container(
                      height: 50,
                      width: 50,
                      alignment: Alignment.centerRight,
                      color: AppColor.divigrey,
                      child: Icon(
                        isobscue ? Icons.visibility_off : Icons.visibility,
                        size: 20,
                        color: AppColor.fontgrey,
                      ),
                    ),
                  ))
            ],
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 10),
            child: LoginTextFieldView(
              hitText: "请确认登录密码",
              controller: _controller4,
              focusNode: _focusNode4,
              isobser: true,
            ),
          ),
          LoginTextFieldView(
            hitText: "请输入邀请码(可不填)",
            controller: _controller2,
            focusNode: _focusNode2,
          ),
          BottomButton(
            title: '注册',
            margin: const EdgeInsets.only(top: 20),
            onTap: _submit,
          ),
        ],
      ),
    );
  }
}
